<?php

namespace app\admin\controller\census;

use app\common\controller\Backend;
use think\Session;
use think\Db;

/**
 * 店铺管理
 *
 * @icon fa fa-circle-o
 */
class Accounting extends Backend
{

    /**
     * Accounting模型对象
     * @var \app\admin\model\census\Accounting
     */
    protected $model = null;
    protected $order_model = null;
    protected $goods_order_model = null;
    protected $order_item_model = null;
    protected $count_order_model = null;
    protected $shop_id = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->shop_id = Session::get('shop');
        $this->model = new \app\admin\model\census\Accounting;
        $this->order_model = new \app\admin\model\order\Order;
        $this->goods_order_model = new \app\admin\model\order\Goods;
        $this->order_item_model = new \app\admin\model\order\Item;
        $this->count_order_model = new \app\admin\model\census\Adate;
        $this->view->assign("stateList", $this->model->getStateList());
        $this->view->assign("verifyList", $this->model->getVerifyList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("typeList", $this->model->getTypeList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['order'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            if ($list->total() > 0) {
                $list_data = $list->items();
                foreach ($list_data as $key => &$value) {
                    $conditions = [];
                    $conditions['o.shop_id'] = $value['id'];
                    $conditions['o.state'] = ['in',['2','3','4','5','6']];//订单状态:1=待支付,2=待发货,3=待收货,4=待评论,5=售后订单(已弃用),6=已完成,7=已取消
                    $conditions['o.status'] = 'normal';
                    $conditions['p.type'] = '0';
                    $conditions['o.dealtime'] = [['>=',$value['order']['adatetime']],['<=',$value['order']['adatetime'] + 3600 * 24 - 1]];

                    //记账总额
                    $value['payment_amount'] = $this->order_model->alias('o')
                                                ->join('wanlshop_pay_out_trade po','po.order_id = o.id','left')
                                                ->join('pay p','p.id = po.pay_way_id','left')
                                                ->where($conditions)
                                                ->sum('o.actual_price');

                    //记账单(笔)数
                    $value['payment_count'] = $this->order_model->alias('o')
                                                ->join('wanlshop_pay_out_trade po','po.order_id = o.id','left')
                                                ->join('pay p','p.id = po.pay_way_id','left')
                                                ->where($conditions)
                                                ->count('o.id');
                }
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 详情
     */
    public function detail()
    {
        $params = $this->request->get();
        $shop_id = $params['shop_id'];
        $createtime = $params['createtime'];

        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $params = $this->request->get();
            $shop_id = $params['shop_id'];
            $createtime = $params['createtime'];
            $conditions = [];
            $conditions['o.shop_id'] = $shop_id;
            $conditions['o.state'] = '6';//订单状态:1=待支付,2=待发货,3=待收货,4=待评论,5=售后订单(已弃用),6=已完成,7=已取消
            $conditions['o.status'] = 'normal';
            $conditions['o.dealtime'] = [['>=',$createtime],['<=',$createtime + 3600 * 24 - 1]];

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            if (isset($params['filter']) && !empty($params['filter'])) {
                $filter = json_decode($params['filter'],true);
                if (isset($filter['order_no'])) {
                    $conditions['o.order_no'] =['like','%'.$filter['order_no'].'%'];
                }

                if (isset($filter['pay_way_id'])) {
                    $conditions['po.pay_way_id'] = $filter['pay_way_id'];
                }
            }

            $list = $this->order_model->alias('o')
                    ->field('IFNULL(og.id,oi.id) as id,o.order_no,o.dealtime,u.nickname,o.actual_price,po.pay_way_id,IFNULL(og.gdtype,"2") as goods_type,p.name')
                    ->join('user u','u.id = o.user_id','left')
                    ->join('wanlshop_order_goods og','og.order_id = o.id','left')
                    ->join('wanlshop_order_item oi','oi.order_id = o.id','left')
                    ->join('wanlshop_pay_out_trade po','po.order_id = o.id','left')
                    ->join('pay p','p.id = po.pay_way_id','left')
                    ->where($conditions)
                    ->paginate($limit);

            $total = $list->total();
            $rows = $list->items();

            if ($total > 0) {
                foreach ($rows as $key => &$value) {
                    $value['j_url'] = url('/order/goods/detail/ids/'.$value["id"]);
                    if ($value['goods_type'] == '1') {
                        $value['j_url'] = url('/order/treatment/detail/ids/'.$value["id"]);
                    }elseif ($value['goods_type'] == '2') {
                        $value['j_url'] = url('/order/item/detail/ids/'.$value["id"]);
                    }
                }
            }

            $result = array("total" => $total, "rows" => $rows);
            return json($result);
        }
        $this->assignconfig('shop_id', $shop_id);
        $this->assignconfig('createtime', $createtime);
        return $this->view->fetch();
    }
}
